package com.bulletelc.apollopre.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;

/**
 * author: lhl
 * date  : 2017/11/04
 * desc  : 图片选择工具
 *
 * 注意危险权限的处理
 */

public final class ImagePickerUtil {

    /**
     * 剪裁图片的临时保存路径
     */
    private static final String tempCropPath = "file:///sdcard/temp";
//    private static final String tempCropPath = "file:///sdcard/temp.png";
//    private static final String tempCropPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/temp/"+System.currentTimeMillis()+".png";

    /**
     * 照片名的前缀
     */
    private static final String PREFIX_PICTURE_NAME="IMG_";

    /**
     * 请求码  打开图库
     */
    public static final int REQUEST_CODE_PHOTO=511;

    /**
     * 请求码  打开相机
     */
    public static  final int REQUEST_CODE_CAMERA=512;

    /**
     * 请求码 打开裁剪
     */
    public static final int REQUEST_CODE_CROP=513;

    private ImagePickerUtil() {
        //empty
    }

    public static String getTempPath() {
        return tempCropPath;
    }


    /**
     * 调用系统相机获取图片,指定照片保存的路径
     * 注意获取相关权限
     * @param activity
     */
    public static void getPhotoFromCamera(Activity activity) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File cameraFile = null;
        String imgName=PREFIX_PICTURE_NAME+DateUtil.filenameByDate(System.currentTimeMillis())+".png";
        if (SDCardUtil.isSDCardMounted()) {
            cameraFile = new File(PathUtil.getExternalPublicCameraDir() +imgName);
            if (!cameraFile.exists()) {
                cameraFile.mkdirs();
            }
        } else {
            cameraFile = new File(PathUtil.getInternalPrivateCameraDir() +imgName);
            if (!cameraFile.exists()) {
                cameraFile.mkdirs();
            }
        }
        LogUtil.i(cameraFile.getAbsolutePath());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));//指定输出路径
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());//输出格式
        activity.startActivityForResult(intent,REQUEST_CODE_CAMERA);
    }

    /**
     * 调用系统相机取图片,使用默认的照片保存路径
     * 注意获取相关权限
     * @param activity
     */
    public static void getPhotoFromCamera2(Activity activity) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        activity.startActivityForResult(intent,REQUEST_CODE_CAMERA);
    }


    /**
     * 调用系统相册,获取图片
     * 需获取读取外部设备权限
     * @param activity
     */
    public static void getSystemPhoto(Activity activity) {
        Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        //intent.putExtra("return-data",true);
        activity.startActivityForResult(intent,REQUEST_CODE_PHOTO);
    }


    /**
     * 调用系统裁剪功能 裁剪图片
     * @param activity
     * @param uri  照片的URI地址
     * @param size 剪裁的大小,单位:pix
     */
    public static void cropPhoto(Activity activity,Uri uri,int size) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop","true"); //启动的intent中显示的view可以裁剪

        // aspectX,aspectY 是宽高的比例
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);

        //outputX,outputY 是裁剪图片的宽高
        intent.putExtra("outputX",size);
        intent.putExtra("outputY",size);

//        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempCropPath));//输出路径
//        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());//输出格式
        if (FileUtil.getFileExtension(uri.getPath()) == "png") {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempCropPath+".png"));//输出路径
            intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());//输出格式
        } else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(tempCropPath+".jpg"));//输出路径
            intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());//输出格式
        }

        intent.putExtra("noFaceDetection", true);//不启动人脸识别
        intent.putExtra("return-data", true);

        activity.startActivityForResult(intent, REQUEST_CODE_CROP);

    }

    /**
     * 选择图片,使用PhotoPicker库
     * @param photoCount 一次性选择图片的数量,取值范围1-9
     * @param activity  获取图片选择后回调的activity,在activity的onActivityResult()方法接收结果
     */
    public static void selectImage(Activity activity,int photoCount) {
        PhotoPicker.builder()
                .setPhotoCount(photoCount)
                .setShowCamera(true)
                .setShowGif(false)
                .setPreviewEnabled(true)
                .setGridColumnCount(3)
                .start(activity,REQUEST_CODE_PHOTO);
    }

    /**
     * 图片预览,使用PhotoPicker库
     * @param activity
     * @param photoPaths  预览的图片路径集合
     * @param currentItem  设置预览界面当前显示的图片序号
     */
    public static void previewImage(Activity activity,ArrayList<String> photoPaths,int currentItem) {
        if (photoPaths == null || photoPaths.size() <= 0) return;

        PhotoPreview.builder()
                .setPhotos(photoPaths)
                .setCurrentItem(currentItem)
                .setShowDeleteButton(false)
                .start(activity,REQUEST_CODE_PHOTO);
    }
}
